package com.gaotong.science.controller;


import com.gaotong.science.entity.History;
import com.gaotong.science.service.HistoryService;
import com.gaotong.science.service.ScienceService;
import com.gaotong.science.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/upload")
public class addFile {

    @Autowired
    HistoryService historyService;
    @Value("${downloadFileUrl}")
    String path;
    @Autowired
    ScienceService scienceService;
    Map<String,String> map=new HashMap<String,String>();
    @PostMapping("/file")
    public Map<String,String> upload(@RequestParam("file") MultipartFile file){
        String url = scienceService.upload(file);
        map.put(file.getOriginalFilename(),url);
        return map;

    }
    @DeleteMapping("/deleteFile/{name}")
    public R deleteFile(@PathVariable String name){
        if(map.get(name)!=null){
            map.remove(name);
        }
        File file= new File(path+name);
        if(file.exists()){
            boolean flag = file.delete();
            return R.ok();
        }
        return R.error();

    }

    @PostMapping("/addfile")
    public R uploadMultipartfile(@RequestParam("filename1")String file1,@RequestParam("filename2") String file2,@RequestParam("period") String date,@RequestParam("outputFileType")String type) {

//        R upload1 = scienceService.upload(file1);
//        R upload2 = scienceService.upload(file2);
//        String url1 = (String) upload1.getData().get("url");
//        String url2 = (String) upload2.getData().get("url");
        String url1 = (String) map.get(file1);
        String url2 = (String) map.get(file2);
        System.out.println(type);
        System.out.println(url1);
        System.out.println(url2);
        System.out.println(date);
        if(url1.equals("")||url2.equals("")|| StringUtils.isEmpty(url1) || StringUtils.isEmpty(url1)){
            return R.error().message("未找到文件");
        }
        if (type.equals("0")) {
            System.out.println(url1);
            System.out.println(url2);
            String path = "/root/zheng/06_summary.py";
            String[] args = new String[]{"python3.8", path, url1, url2, date};
            R mess = Python.runPython(args);
            if(mess.getCode()==200){
                String url = (String)mess.getData().get("url");
                String[] split = url.split("-");
                String year = split[0];
                String month = split[1];
                String name=split[2];
                History history = new History();
                historyService.saveOrUpdate(history);
            }
            return mess;
//           return R.ok();

        } else if (type.equals("1")) {
            System.out.println(url1);
            System.out.println(url2);
            String path = "/root/zheng/10_summary.py";
            String[] args = new String[]{"python3.8", path, url1, url2, date};
            R mess = Python.runPython(args);
            if(mess.getCode()==200){
                String url = (String)mess.getData().get("url");
//                System.out.println(url);
//                String[] split = url.split("-");
//                String year = split[0];
//                System.out.println(year);
//                String month = split[1];
//                System.out.println(month);
//                String name=split[2];
//              String time=split[0]+"-"+split[1];
////                System.out.println(name);
//                History history = new History();
//                history.setTime(time);
//                history.setName(url);
                historyService.save1(url);
            }
            return mess;

        } else {
            return R.error().message("未选择类型");
        }


    }
}

